package http;

import android.content.Context;

import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import log.LogManager;

/**
 * 处理原始返回信息
 */
public class BaseJsonHttpResponseParse extends JsonHttpResponseHandler {
    private Context mContext;
    private Map<Object, Object> map;
    private String url;

    public BaseJsonHttpResponseParse() {
    }

    public BaseJsonHttpResponseParse(Context context) {
        this.mContext = context;
    }

    public BaseJsonHttpResponseParse(Context context, String url) {
        this.mContext = context;
        this.url = url;
    }

    public BaseJsonHttpResponseParse(Context context, Map<Object, Object> map) {
        this.mContext = context;
        this.map = map;
    }


    /********************** end 重复点击请求处理，当响应成功后延迟1秒关闭*************************/

    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
        super.onSuccess(statusCode, headers, response);
        closeServerDialog(response);
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
        super.onFailure(statusCode, headers, responseString, throwable);
        backFailure();
        LogManager.getLogger().d("onFailure", throwable.getMessage());
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
        super.onFailure(statusCode, headers, throwable, errorResponse);
        backFailure();
        LogManager.getLogger().d("onFailure", String.valueOf(throwable.getMessage()));
        closeServerDialog(errorResponse);
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
        super.onFailure(statusCode, headers, throwable, errorResponse);
        backFailure();

    }

    @Override
    public void onFinish() {
        super.onFinish();
    }


    /**请求服务提示*/
    private void closeServerDialog(JSONObject errorResponse){
        int code;
        try {
            if (null != errorResponse) {
                code = errorResponse.getInt("code");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    /**关闭应用退出登录*/
    public void closeServer() {

    }

    /**
     * 失败的返回
     */
    public void backFailure() {

    }

    public Map<Object, Object> getMap() {
        return map;
    }

    public void setMap(Map<Object, Object> map) {
        this.map = map;
    }
}
